// owner-app/pages/perfectInformation/index.js
import { Subscribe } from "../../../models/subscribe";
const subscribe = new Subscribe();
import { handlerGobackClick, onlyChangeRouter } from "../../../utils/util";
const app = getApp();
Page({
  data: {
    baseImgUrl: "",
    activeTab: 0,
    scrollH: "auto",
    tabOne0: [],
    tabOne1: [],

    //加载中状态
    loading: false,
    loading1: false,
    //没有更多了
    noMore: false,
    noMore1: false,
    //加载失败
    loadingFailed: false,
    loadingFailed1: false,
    //当前已加载页数
    pageNo: 1,
    pageNo1: 1,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var query = wx.createSelectorQuery();
    query
      .select("#scrollH")
      .boundingClientRect((res) => {
        let height = res.height - 113;
        // let height = res.height - 44;
        this.setData({
          scrollH: height,
        });
      })
      .exec();
  },
  handlerGobackClick,
  onlyChangeRouter,
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var { baseImgUrl } = getApp().globalImgUrl.config;
    this.setData({
      baseImgUrl,
    });

    this.initPageData();
  },
  subscribeCodeTap(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/owner-app/pages/subscribe-code/index?id=${id}`,
    });
  },
  scrollBottom() {
    if (!this.data.loading && !this.data.noMore) {
      this.setData({
        loading: true,
        pageNo: this.data.pageNo + 1,
      });
      this.getData(
        "miniVisitorList",
        "loading",
        "tabOne0",
        "pageNo",
        "loadingFailed",
        1,
        "noMore",
        true
      );
    }
  },
  scrollBottom1() {
    if (!this.data.loading1 && !this.data.noMore1) {
      this.setData({
        loading1: true,
        pageNo1: this.data.pageNo1 + 1,
      });
      this.getData(
        "miniVisitorList",
        "loading1",
        "tabOne1",
        "pageNo1",
        "loadingFailed1",
        2,
        "noMore1",
        true
      );
    }
  },
  onChange(event) {
    // wx.showToast({
    //   title: `切换到标签 ${event.detail.name}`,
    //   icon: 'none',
    // });
  },
  /**
   *
   * @param methodName   同类下面的方法名称
   * @param loadingName  加载状态变量
   * @param dataName     data中设定的数据
   * @param isPage       是否是分页查询
   */
  getData(
    methodName,
    loadingName,
    dataName,
    pageNoName,
    loadingFailedName,
    type,
    noMoreName,
    isPage
  ) {
    let data = {
      type,
      id: wx.getStorageSync("user").memberId,
      pageNo: this.data[pageNoName],
      pageSize: 10,
    };

    subscribe[methodName](data)
      .then((res) => {
        this.setData({
          [loadingName]: false,
        });

        if (res.result.pages * res.result.size >= res.result.total) {
          this.setData({
            [noMoreName]: true,
          });
          return false;
        }

        //有分页
        if (isPage) {
          this.setData({
            [dataName]: this.data[dataName].concat(res.result.records.records),
          });
        }
        //没有分页就直接赋值
        else {
          this.setData({
            [dataName]: res.result.records.records,
          });
        }
      })
      .catch((err) => {
        this.setData({
          [loadingFailedName]: true,
        });
      });
  },
  initPageData() {
    let data = {
      type: 1,
      id: wx.getStorageSync("user").memberId,
      pageNo: 1,
      pageSize: 10,
    };

    let data2 = {
      type: 2,
      id: wx.getStorageSync("user").memberId,
      pageNo: 1,
      pageSize: 10,
    };

    subscribe.miniVisitorList(data).then((res) => {
      if (res.code == 1000) {
        this.setData({
          tabOne0: res.result.records,
        });
      }
    });

    subscribe.miniVisitorList(data2).then((res) => {
      if (res.code == 1000) {
        this.setData({
          tabOne1: res.result.records,
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
